﻿
CREATE PROCEDURE [dbo].[proc_Assets_Update_G]
	(	
		@CompanyId int,
		@DepartmentId int,
		@UserName nvarchar(50),
		@EName nvarchar(50),
		@StateId int,	
		@DJUserName nvarchar(50),
		@DJDate datetime,	
		@GId uniqueidentifier,
		@zcbh VARCHAR(50),
		@memo NVARCHAR(2000),
		@userid VARCHAR(30),
		@username_1 NVARCHAR(50)
	)
AS
	--Update Assets Set
	--	DepartmentId=@DepartmentId,
	--	UserName=@UserName,
	--	EName=@EName,
	--	StateId=@StateId,
	--	DJUserName=@DJUserName,
	--	DJDate=@DJDate,
	--	LyDate=getdate()
		
	--	Where GId=@GId
	--	--
	--	if(@EName = '')
	--		begin
	--			update Assets set LyDate=NULL Where GId=@GId 
	--		END
			
--	IF 	(@EName = '')
--	BEGIN
--		
--		Update Assets Set
--		DepartmentId=@DepartmentId,
--		UserName=@UserName,
--		EName=@EName,
--		StateId=@StateId,
--		DJUserName=@DJUserName,
--		DJDate=@DJDate,
--		LyDate=NULL,
--		CompanyId = @CompanyId
--		
--		Where GId=@GId
--	END
--	ELSE
--		BEGIN
--		Update Assets Set
--		DepartmentId=@DepartmentId,
--		UserName=@UserName,
--		EName=@EName,
--		StateId=@StateId,
--		DJUserName=@DJUserName,
--		DJDate=@DJDate,
--		LyDate=getdate(),
--		oldEName = @EName,
--		oldUserName = @UserName,
--		CompanyId = @CompanyId
--		
--		Where GId=@GId
--		END	
--						
--INSERT INTO [zcjl]
--           ([zcbh]
--           ,[memo]
--           ,[lrrq]
--           ,[userid]
--           ,[username])
--     VALUES
--           (@zcbh
--           ,@memo
--           ,GETDATE()
--           ,@userid
--           ,@username_1)			
--	RETURN

--2013-03-30 By Zht
	IF 	(@EName = '')
	BEGIN
		
		Update a Set
		a.DepartmentId=@DepartmentId,
		a.UserName=@UserName,
		a.EName=@EName,
		a.StateId=@StateId,
		a.DJUserName=@DJUserName,
		a.DJDate=@DJDate,
		a.LyDate=NULL,
		a.oldEName = b.ename,    
        a.oldUserName = b.username,
		a.CompanyId = @CompanyId
		FROM Assets a,Assets b
		WHERE a.id=b.id and a.GId=@GId
	END
	ELSE
		BEGIN
		Update a Set
		a.DepartmentId=@DepartmentId,
		a.UserName=@UserName,
		a.EName=@EName,
		a.StateId=@StateId,
		a.DJUserName=@DJUserName,
		a.DJDate=@DJDate,
		a.LyDate=getdate(),
		a.oldEName = b.ename,    
        a.oldUserName = b.username,
		a.CompanyId = @CompanyId
		FROM Assets a,Assets b
		WHERE a.id=b.id and a.GId=@GId
		END	
						
INSERT INTO [zcjl]
           ([zcbh]
           ,[memo]
           ,[lrrq]
           ,[userid]
           ,[username])
     VALUES
           (@zcbh
           ,@memo
           ,GETDATE()
           ,@userid
           ,@username_1)			
	RETURN